package com.zhiyou.hospital.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhiyou.hospital.ssh.model.RoleModel;
import com.zhiyou.hospital.ssh.model.UserModel;
import com.zhiyou.hospital.ssh.service.IRoleService;
import com.zhiyou.hospital.ssh.service.IUserService;
import com.zhiyou.hospital.ssh.util.LogHelper;
import com.zhiyou.hospital.ssh.util.LogUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhouyanchao on 2017/10/18.
 * Descr: 用户管理模块
 */
@Namespace("/user")
@ParentPackage("defaultPackage")
public class UserAction extends BaseAction {
	//接收添加用户时传的参数
	UserModel user;
	LogUtil log = LogHelper.getLogUtilInstance(UserAction.class);

	public UserModel getUser() {
		return user;
	}

	public void setUser(UserModel user) {
		this.user = user;
	}


	@Autowired
	IUserService userService;

	@Autowired
	IRoleService roleService;

	@Action(value = "add",results = {@Result(name = "success",location = "/WEB-INF/jsp/user_add.jsp"),
				@Result(name = "result",location = "/WEB-INF/jsp/test.jsp")})
	public String addTest(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String method=request.getMethod();

		if(method.equalsIgnoreCase(REQ_GET)){
			log.i("准备添加用户");

			//获取所有角色
			List<RoleModel> roles = roleService.queryAllRoles();
			ActionContext.getContext().put("roles",roles);

			return SUCCESS;
		}

		if(method.equalsIgnoreCase(REQ_POST)){
			//处理添加用户信息
			System.out.println(user);
			System.out.println(user.getRoleModel());
//
//			RoleModel role = roleService.getById(RoleModel.class,user.getRoleModel().getId());
//			user.setRoleModel(role);

			userService.addNewData(user);


			return "result";
		}

		return SUCCESS;
	}

}
